﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;

namespace POSPlus.EZBarTender
{
    public class SpecialNormalizer : RegularNormalizer
    {
        public SpecialNormalizer() : base()
        {
            LabelInformation.Columns.Add("SpecialPriceLine1");
            LabelInformation.Columns.Add("SpecialPriceLine2");
            LabelInformation.Columns.Add("SpecialPriceLine3");
            LabelInformation.Columns.Add("SpecialBarcodeCheckbit");
            LabelInformation.Columns.Add("SpecialBarcodeNoCheckbit");
            LabelInformation.Columns.Add("WhereToBuy");
            LabelInformation.Columns.Add("PalletNo");
            LabelInformation.Columns.Add("Start_Date");
            LabelInformation.Columns.Add("End_Date");
            LabelInformation.Columns.Add("USave");
            LabelInformation.Columns.Add("Unit_SP");
            LabelInformation.Columns.Add("Rehang");
        }

        public override void Normalize(DataTable table)
        {

            foreach (DataRow row in table.Rows)
            {
                DataRow informationRow = LabelInformation.NewRow();
                informationRow["Description"] = row["Description"];
                informationRow["Vendor"] = row["Vendor"];
                try
                {
                    informationRow["CircleFile"] = CircleFile(int.Parse(row["Vendor"].ToString()));
                }
                catch (FormatException ex)
                {
                    informationRow["CircleFile"] = ConfigurationManager.AppSettings["Blank"];
                    Console.WriteLine("CircleFile: " + row["Vendor"].ToString() + ", " + ex.Message);
                }
                informationRow["Cert_Code"] = row["Cert_Code"];
                try
                {
                    informationRow["AvgDailyMovement"] = AvgDailyMovement(double.Parse(row["AvgDailyMovement"].ToString()));
                }
                catch (FormatException ex)
                {
                    informationRow["AvgDailyMovement"] = "";
                    Console.WriteLine("AvgDailyMovement: " + row["AvgDailyMovement"].ToString() + ", " + ex.Message);
                }
                informationRow["Pack"] = row["Pack"];
                informationRow["Size"] = row["Size"];
                try
                {
                    informationRow["Unit_NP"] = Unit_NP(double.Parse(row["Unit_NP"].ToString()));
                }
                catch (FormatException ex)
                {
                    informationRow["Unit_NP"] = "";
                    Console.WriteLine("Unit_NP: " + ex.Message);
                }
                informationRow["UOM"] = row["UOM"];
                try
                {
                    informationRow["RegularUpcCheckbit"] =
                        RegularUpcCheckBit(
                        int.Parse(row["Vendor"].ToString()),
                        int.Parse(row["Cert_Code"].ToString()));
                }
                catch (FormatException ex)
                {
                    informationRow["RegularUpcCheckBit"] = "";
                    Console.WriteLine("RegualarUpcCheckBit: " + ex.Message);
                }
                try
                {
                    informationRow["RegularUpcNoCheckbit"] =
                        RegularUpcNoCheckBit(
                        int.Parse(row["Vendor"].ToString()),
                        int.Parse(row["Cert_Code"].ToString()));
                }
                catch (FormatException ex)
                {
                    informationRow["RegularUpcNoCheckbit"] = "";
                    Console.WriteLine("RegularUpcNoCheckbit: " + ex.Message);
                }
                try
                {
                    informationRow["Authorized"] = Authorized(int.Parse(row["Hosted"].ToString()));
                }
                catch (FormatException ex)
                {
                    informationRow["Authorized"] = "";
                    Console.WriteLine("Authorized: " + ex.Message);
                }
                try
                {
                    informationRow["RegularPriceLine1"] =
                        PriceLine1(
                            row["PriceMethod"].ToString(), row["Quantity"].ToString(), row["Normal_Price"].ToString(), row["GroupPrice"].ToString());
                }
                catch (FormatException ex)
                {
                    informationRow["RegularPriceLine1"] = "";
                    Console.WriteLine("RegularPriceLine1: " + ex.Message);
                }
                try
                {
                    informationRow["RegularPriceLine2"] =
                        PriceLine2(row["PriceMethod"].ToString(), row["Quantity"].ToString(), row["Normal_Price"].ToString(), row["GroupPrice"].ToString());
                }
                catch (FormatException ex)
                {
                    informationRow["RegularPriceLine2"] = "";
                    Console.WriteLine("RegularPriceLine2: " + ex.Message);
                }
                try
                {
                    informationRow["RegularPriceLine3"] =
                        PriceLine3(row["PriceMethod"].ToString(),
                            row["Quantity"].ToString(),
                            row["Normal_Price"].ToString(),
                            row["GroupPrice"].ToString());
                }
                catch (FormatException ex)
                {
                    informationRow["RegularPriceLine3"] = "";
                    Console.WriteLine("RegularPriceLine3: " + ex.Message);
                }

                informationRow["UPC"] = row["UPC"];
                informationRow["LocAisle"] = row["LocAisle"];
                informationRow["LocBlock"] = row["LocBlock"];
                informationRow["LocSeq"] = row["LocSeq"];
                informationRow["PrintDate"] = DateTime.Now.Date;
                informationRow["BatchNo"] = BatchNo(row["BatchNo"].ToString());

                /*
                 * Special Section
                 */
                informationRow["SpecialPriceLine1"] = PriceLine1(
                    row["SpecialPriceMethod"].ToString(),
                    row["SpecialQuantity"].ToString(),
                    row["Special_Price"].ToString(),
                    row["SpecialGroupPrice"].ToString());
                informationRow["SpecialPriceLine2"] = PriceLine2(
                    row["SpecialPriceMethod"].ToString(),
                    row["SpecialQuantity"].ToString(),
                    row["Special_Price"].ToString(),
                    row["SpecialGroupPrice"].ToString());
                informationRow["SpecialPriceLine3"] = PriceLine3(
                    row["SpecialPriceMethod"].ToString(),
                    row["SpecialQuantity"].ToString(),
                    row["Special_Price"].ToString(),
                    row["SpecialGroupPrice"].ToString());

                informationRow["SpecialBarcodeCheckbit"] = SpecialBarcodeCheckbit(
                    row["Vendor"].ToString(),
                    row["PalletCode"].ToString(),
                    row["Cert_Code"].ToString());

                informationRow["SpecialBarcodeNoCheckbit"] = SpecialBarcodeNoCheckbit(
                    row["Vendor"].ToString(),
                    row["PalletCode"].ToString(),
                    row["Cert_Code"].ToString());

                informationRow["WhereToBuy"] = row["WhereToBuy"];
                informationRow["PalletNo"] = PalletNo(row["PalletCode"].ToString(), row["Cert_Code"].ToString());
                informationRow["Start_Date"] = BHStart_Date(row["Start_Date"].ToString());
                informationRow["End_Date"] = BHEnd_Date(row["End_Date"].ToString());
                informationRow["End_Date"] = DSDEnd_Date(row["DealEndDate"].ToString(), informationRow["End_Date"].ToString());
                informationRow["USave"] = USave(
                    row["Normal_Price"].ToString(),
                    row["PriceMethod"].ToString(),
                    row["GroupPrice"].ToString(),
                    row["Quantity"].ToString(),
                    row["Special_Price"].ToString(),
                    row["SpecialPriceMethod"].ToString(),
                    row["SpecialGroupPrice"].ToString(),
                    row["SpecialQuantity"].ToString());
                try
                {
                    informationRow["Unit_SP"] = Unit_NP(double.Parse(row["Unit_SP"].ToString()));
                }
                catch (FormatException ex)
                {
                    Console.WriteLine("Unit_SP: " + row["Unit_SP"].ToString() + ", " + ex.Message);
                }

                LabelInformation.Rows.Add(informationRow);
                Console.WriteLine(LabelInformation.Rows[0]["Description"].ToString());
            }
        }

        public String SpecialBarcodeCheckbit(String sVendor, String sPalletCode, String sCert_Code)
        {
            int vendor = -1;
            int.TryParse(sVendor, out vendor);

            if (vendor == 1 || vendor == 351)
            {
                if (sPalletCode != null && sPalletCode.Length > 0)
                    return sPalletCode;
                else
                    return sCert_Code;
            }
            else
            {
                return "";
            }
        }

        public String SpecialBarcodeNoCheckbit(String sVendor, String sPalletCode, String sCert_Code)
        {
            int vendor = -1;
            int.TryParse(sVendor, out vendor);

            if (vendor == 3)
            {
                if (sPalletCode != null && sPalletCode.Length > 0)
                    return sPalletCode;
                else
                    return sCert_Code;
            }
            else
                return "";
        }

        public String PalletNo(String sPalletCode, String sCert_Code)
        {
            if (sPalletCode != null && sPalletCode.Length > 0)
                return String.Format("P#{0}", sPalletCode);
            else
                return String.Format("i#{0}", sCert_Code);
        }

        public String USave(String normal_price, String priceMethod, String groupPrice, String quantity,
            String special_price, String specialPriceMethod, String specialGroupPrice, String specialQuantity)
        {
            double regPrice = -1;
            double specPrice = -1;
            int convertedPriceMethod = -1;
            int convertedSpecialPriceMethod = -1;
            double convertedNormal_Price = -1;
            double convertedGroupPrice = -1;
            int convertedQuantity = -1;
            double convertedSpecial_Price = -1;
            double convertedSpecialGroupPrice = -1;
            int convertedSpecialQuantity = -1;

            int.TryParse(priceMethod, out convertedPriceMethod);
            int.TryParse(specialPriceMethod, out convertedSpecialPriceMethod);
            double.TryParse(normal_price, out convertedNormal_Price);
            double.TryParse(groupPrice, out convertedGroupPrice);
            int.TryParse(quantity, out convertedQuantity);

            double.TryParse(special_price, out convertedSpecial_Price);
            double.TryParse(specialGroupPrice, out convertedSpecialGroupPrice);
            int.TryParse(specialQuantity, out convertedSpecialQuantity);

            switch (convertedPriceMethod)
            {
                case 0: regPrice = convertedNormal_Price; break;
                case 1:
                case 2: regPrice = convertedGroupPrice / convertedQuantity; break;
                case 3:
                case 4: regPrice = convertedGroupPrice; break;
            }

            switch (convertedSpecialPriceMethod)
            {
                case 0: specPrice = convertedSpecial_Price; break;
                case 1:
                case 2: specPrice = convertedSpecialGroupPrice / convertedSpecialQuantity; break;
                case 3:
                case 4: specPrice = convertedSpecialGroupPrice; break;
            }

            double uSave = regPrice - specPrice;

            return ConvertMoney(uSave);
        }

        public String BHStart_Date(String start_date)
        {
            DateTime convertedStartDate = new DateTime(2100, 1, 1);
            DateTime.TryParse(start_date, out convertedStartDate);

            if (convertedStartDate < new DateTime(2004, 1, 1))
            {
                return DateTime.Now.ToString("MM/dd/yyyy");
            }
            else
                return convertedStartDate.ToString("MM/dd/yyyy");
        }

        public String BHEnd_Date(String end_date)
        {
            DateTime convertedEndDate = new DateTime(1900, 1, 1);
            DateTime.TryParse(end_date, out convertedEndDate);

            if (convertedEndDate > DateTime.Now.AddDays(360))
                return (DateTime.Now.AddDays(180)).ToString("MM/dd/yyyy");
            else
                return convertedEndDate.ToString("MM/dd/yyyy");
        }

        public String DSDEnd_Date(String dealEndDate, String end_date)
        {
            if (dealEndDate == null || dealEndDate.Length < 1)
                return end_date;
            else
                return DateTime.Parse(dealEndDate).ToString("MM/dd/yyyy");
        }
    }
}
